#include <stdio.h>
#include <string.h>

int read_file_normal(const char* filename)
{
	FILE* f = fopen(filename, "rt");
	char buf[100];

	if (f == NULL) {
		fprintf(stderr, "Failed to open %s", filename);
		goto error_open;
	}

	if (fread(buf, sizeof(buf), 1, f) == 0) {
		fprintf(stderr, "Failed to read data from %s\n", filename);
		goto error_read;
	}

	if (fclose(f) == EOF) {
		fprintf(stderr, "Failed to close %s\n", filename);
	}

	return 0;

error_read:
	fclose(f);
error_open:
	return -1;
}

int read_file_wrong_order(const char* filename)
{
	FILE* f = fopen(filename, "rt");
	char buf[100];

	if (f == NULL) {
		fprintf(stderr, "Failed to open %s", filename);
		goto error_open;
	}

	if (fread(buf, sizeof(buf), 1, f) == 0) {
		fprintf(stderr, "Failed to read data from %s\n", filename);
		goto error_read;
	}

	if (fclose(f) == EOF) {
		fprintf(stderr, "Failed to close %s\n", filename);
	}

	return 0;

error_open:
	fclose(f);
error_read:
	return -1;
}

int read_file_repeated_goto(const char* filename)
{
	FILE* f = fopen(filename, "rt");
	char buf[100];

	if (f == NULL) {
		fprintf(stderr, "Failed to open %s", filename);
		goto error_open;
	}

	if (fread(buf, sizeof(buf), 1, f) == 0) {
		fprintf(stderr, "Failed to read data from %s\n", filename);
		goto error_open;
	}

	if (fclose(f) == EOF) {
		fprintf(stderr, "Failed to close %s\n", filename);
	}

	return 0;

error_open:
	fclose(f);
	return -1;
}

int read_file_extra_goto(const char* filename)
{
	FILE* f = fopen(filename, "rt");
	char buf[100];

	if (f == NULL) {
		fprintf(stderr, "Failed to open %s", filename);
		goto error_open;
	}

again:
	if (fread(buf, sizeof(buf), 1, f) == 0) {
		fprintf(stderr, "Failed to read data from %s\n", filename);
		goto error_read;
	}
	if (buf[0] == 0) {
		goto again;
	}

	if (fclose(f) == EOF) {
		fprintf(stderr, "Failed to close %s\n", filename);
	}

	return 0;

error_read:
	fclose(f);
error_open:
	return -1;
}

int main(int argc, char* argv[])
{
	if (argc < 2) {
		fprintf(stderr, "Too few arguments\n");
		goto err_arg_count;
	}

	if (strcmp(argv[1], "--asdf") != 0) {
		fprintf(stderr, "Invalid argument");
		goto err_not_asdf;
	}
	return 0;
err_not_asdf:
err_arg_count:
	return 0;
}
